package com.luhai.shipmentscheck.manager;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

/**
 * Created by jzh-创世头盔之父.
 * Date: 2019\10\26 0026* Time: 11:01
 * Emaile: 1186832904@qq.com
 * Introduction: 传感器封装类
 */
public class MySensorManager implements SensorEventListener {

    private static volatile MySensorManager singleTon;

    private SensorManager mSensroMgr;

    private Context mContext;

    private OnSensorChangeListener onSensorChangeListener;

    public interface OnSensorChangeListener{

        void onLightChangeListener(float light);
    }

    private MySensorManager() {
    }

    public static MySensorManager getSingleTon() {
        if (singleTon == null) {
            synchronized (MySensorManager.class) {
                if (singleTon == null) {
                    singleTon = new MySensorManager();
                }
            }
        }
        return singleTon;
    }

    public void init(Context context,OnSensorChangeListener onSensorChangeListener) {

        this.mContext = context;
        this.onSensorChangeListener=onSensorChangeListener;
        mSensroMgr = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
        mSensroMgr.registerListener(this, mSensroMgr.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_NORMAL);
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
            float light_strength = event.values[0];
            //Log.i("lightlight", "当前光线强度为" + light_strength);
            if(onSensorChangeListener!=null){
                onSensorChangeListener.onLightChangeListener(light_strength);
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void destroy() {
        if (mSensroMgr != null) {
            mSensroMgr.unregisterListener(this);
        }
    }
}
